const vm = new Vue({
    el: '#main',
    data: {
        getAuthCodeText: '获取验证码',
        getAuthCodeAble: true,
        reqData: {
            tel: '',
            code: '',
            pwd: '',
        },
    },
    methods: {
        telBlur,
        keyup,
        getAuthCode,
    },
});

async function main() {
    fxj.fixStatusBar('.fxj-header');
    fxj.fixContainer('#main');
}

function apiready() {
    main().catch(
        e => {
            console.error(e);
        },
    );
}


function telBlur() {
    vm.reqData.tel = fxj.trimAll(vm.reqData.tel);
    if (!fxj.verifyIpt(vm.reqData.tel, 'tel')) {
        mui.toast('请输入正确手机号', {duration: 'long', type: 'div'});
        banA('.auth-code-btn');
    }
    else {
        unBanA('.auth-code-btn');
    }
}

function banA(elem) {
    const a = document.querySelector(elem);
    a.classList.add('disable');
}

function unBanA(elem) {
    const a = document.querySelector(elem);
    a.classList.remove('disable');
}

function keyup() {
    if (vm.reqData.tel.length === 11) {
        unBanA('.auth-code-btn');
    }
}

function countDown() {
    let second = 60;
    banA('.auth-code-btn');
    setTimeout(function self() {
        second--;
        if (second > 0) {
            vm.getAuthCodeText = `${second}s后重发`;
            setTimeout(self, 1000);
        }
        else {
            vm.getAuthCodeText = '重新发送';
            unBanA('.auth-code-btn');
        }
    }, 1000);
}

async function getAuthCode() {
    const isVip = await isVip(vm.reqData.tel);

    countDown();
}

async function getAuthCode_async() {

}

function isVip(str) {
    return new Promise((resolve) => {
        ajax({
            url: 'home/userLogin/isVip',
            type: 'post',
            data: {
                phone: vm.reqData.tel,
            },
        }).then(
            result => {
                if (result.err !== 0) {
                    mui.toast(result.msg);
                }
                else if (result.err === 0) {
                    if (result.data.isVip === 1) {
                        resolve(true);
                    }
                    else if (result.data.isVip === -1) {
                        return (false);
                    }
                }
            },
        );
    });
}